import { post } from '@/utils/httpRequest'
import { selectMenuListByUserId } from '@/api/api'

export const layout = {
  state: {
    // 菜单列表(数组形式)
    menuList: [],
    // 菜单列表(树结构形式)
    menuTree: [],
    // 菜单展开状态 false收缩，true展开
    menuExpandStatus: false,
    // 路由导航小标题
    navList: JSON.parse(sessionStorage.getItem('navList')) ?? [],
    // 缓存路由导航小标题
    navCacheList: []
  },
  mutations: {
    // 设置路由导航小标题
    setNavList (state, payload) {
      state.navList = payload
      sessionStorage.setItem('navList', JSON.stringify(payload))
    },
    setNavCacheList (state, payload) {
      state.navCacheList = payload
    },
    // 设置menuList
    setMenuList (state, payload) {
      state.menuList = payload.menuList
    },
    // 设置menuTree
    setMenuTree (state, payload) {
      state.menuTree = payload.menuTree
    },
    // 设置菜单状态
    setMenuExpandStatus (state) {
      state.menuExpandStatus = !state.menuExpandStatus
    }
  },
  actions: {
    // 获取当前用户下的菜单列表，并且设置menuList
    async setMenuDataAsync ({
      commit
    }) {
      const result = await post(selectMenuListByUserId, {})
      if (result.code === 200) {
        commit('setMenuTree', {
          menuTree: result.data.menuTree
        })
        commit('setMenuList', {
          menuList: result.data.menuList
        })
      }
    }
  }
}
